home *** CD-ROM | disk | FTP | other *** search
- #define kBaseResID 128
- #define kAboutALRTid 129
- #define kDialogResID 128
-
- #define kVisible true
- #define kMoveToFront (WindowPtr)-1L
- #define kNoGoAway false
- #define kSleep 60L
-
- #define kFirstRadio 3
-
- #define kOn 1
- #define kOff 0
-
- #define iAfghan 3
- #define iElephant 4
- #define iSquirrel 5
-
- #define iShowPreview 7
- #define iUserItem 8
-
- #define kLeftMargin 5
- #define kTopMargin 40
-
- #define mApple kBaseResID
- #define iAbout 1
-
- #define mFile kBaseResID+1
- #define iSettings 1
- #define iQuit 3
-
-
- /*************/
- /* Globals */
- /*************/
-
- Boolean gDone, gShowPreview = true;
- short gCurrentPICT = kBaseResID;
-
-
- /***************/
- /* Functions */
- /***************/
-
- void ToolBoxInit( void );
- PicHandle LoadPICT( short picID );
- void CreateWindow( void );
- void MenuBarInit( void );
- void EventLoop( void );
- void DoEvent( EventRecord *eventPtr );
- void HandleMouseDown( EventRecord *eventPtr );
- void HandleMenuChoice( long menuChoice );
- void HandleAppleChoice( short item );
- void HandleFileChoice( short item );
- void DoUpdate( EventRecord *eventPtr );
- void DoDialog( void );
- void FlipControl( ControlHandle control );
- void DrawPreview( DialogPtr dialog, short picID );
- void SwitchPICT( void );
-
- /* see tech note 304 */
- pascal OSErr SetDialogDefaultItem(DialogPtr theDialog, short newItem)
- = { 0x303C, 0x0304, 0xAA68 };
- pascal OSErr SetDialogCancelItem(DialogPtr theDialog, short newItem)
- = { 0x303C, 0x0305, 0xAA68 };
-
-
- /******************************** main *********/
-
- void main( void )
- {
- ToolBoxInit();
- MenuBarInit();
-
- CreateWindow();
-
- EventLoop();
- }
-
-
- /*********************************** ToolBoxInit */
-
- void ToolBoxInit( void )
- {
- InitGraf( &thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( NULL );
- InitCursor();
- }
-
-
- /******************************** LoadPICT *********/
-
- PicHandle LoadPICT( short picID )
- {
- PicHandle pic;
-
- pic = GetPicture( picID );
-
- if ( pic == NULL )
- {
- SysBeep( 10 ); /* Couldn't load the PICT resource!!! */
- ExitToShell();
- }
- }
-
-
- /******************************** CreateWindow *********/
-
- void CreateWindow( void )
- {
- WindowPtr window;
- PicHandle pic;
- Rect r;
-
- pic = LoadPICT( gCurrentPICT );
-
- r = (**pic).picFrame;
-
- OffsetRect( &r, kLeftMargin - r.left,
- kTopMargin - r.top );
-
- window = NewWindow( NULL, &r, "\pMy Pet Fred", kVisible,
- noGrowDocProc, kMoveToFront, kNoGoAway, 0L );
-
- if ( window == NULL )
- {
- SysBeep( 10 ); /* Couldn't load the WIND resource!!! */
- ExitToShell();
- }
-
- ShowWindow( window );
- SetPort( window );
- }
-
-
- /****************** MenuBarInit ***********************/
-
- void MenuBarInit( void )
- {
- Handle menuBar;
- MenuHandle menu;
-
- menuBar = GetNewMBar( kBaseResID );
- SetMenuBar( menuBar );
-
- menu = GetMHandle( mApple );
- AddResMenu( menu, 'DRVR' );
-
- DrawMenuBar();
- }
-
-
- /******************************** EventLoop *********/
-
- void EventLoop( void )
- {
- EventRecord event;
-
- gDone = false;
- while ( gDone == false )
- {
- if ( WaitNextEvent( everyEvent, &event, kSleep, NULL ) )
- DoEvent( &event );
- }
- }
-
-
- /************************************* DoEvent */
-
- void DoEvent( EventRecord *eventPtr )
- {
- char theChar;
-
- switch ( eventPtr->what )
- {
- case mouseDown:
- HandleMouseDown( eventPtr );
- break;
- case keyDown:
- case autoKey:
- theChar = eventPtr->message & charCodeMask;
- if ( (eventPtr->modifiers & cmdKey) != 0 )
- HandleMenuChoice( MenuKey( theChar ) );
- break;
- case updateEvt:
- DoUpdate( eventPtr );
- break;
- }
- }
-
-
- /************************************* HandleMouseDown */
-
- void HandleMouseDown( EventRecord *eventPtr )
- {
- WindowPtr window;
- short thePart;
- long menuChoice;
- GrafPtr oldPort;
- long windSize;
- Rect growRect;
-
- thePart = FindWindow( eventPtr->where, &window );
-
- switch ( thePart )
- {
- case inMenuBar:
- menuChoice = MenuSelect( eventPtr->where );
- HandleMenuChoice( menuChoice );
- break;
- case inSysWindow :
- SystemClick( eventPtr, window );
- break;
- case inContent:
- SelectWindow( window );
- break;
- case inDrag :
- DragWindow( window, eventPtr->where, &screenBits.bounds );
- break;
- }
- }
-
-
- /****************** HandleMenuChoice ***********************/
-
- void HandleMenuChoice( long menuChoice )
- {
- short menu;
- short item;
-
- if ( menuChoice != 0 )
- {
- menu = HiWord( menuChoice );
- item = LoWord( menuChoice );
-
- switch ( menu )
- {
- case mApple:
- HandleAppleChoice( item );
- break;
- case mFile:
- HandleFileChoice( item );
- break;
- }
- HiliteMenu( 0 );
- }
- }
-
-
- /****************** HandleAppleChoice ***********************/
-
- void HandleAppleChoice( short item )
- {
- MenuHandle appleMenu;
- Str255 accName;
- short accNumber;
-
- switch ( item )
- {
- case iAbout:
- NoteAlert( kAboutALRTid, NULL );
- break;
- default:
- appleMenu = GetMHandle( mApple );
- GetItem( appleMenu, item, accName );
- accNumber = OpenDeskAcc( accName );
- break;
- }
- }
-
-
- /****************** HandleFileChoice ***********************/
-
- void HandleFileChoice( short item )
- {
- short newPICTid;
-
- switch ( item )
- {
- case iSettings:
- DoDialog();
- break;
- case iQuit:
- gDone = true;
- break;
- }
- }
-
-
- /************************************* DoUpdate */
-
- void DoUpdate( EventRecord *eventPtr )
- {
- PicHandle pic;
- WindowPtr window;
- Rect r;
-
- window = (WindowPtr)eventPtr->message;
-
- pic = LoadPICT( gCurrentPICT );
-
- SetPort( window );
-
- BeginUpdate( window );
-
- r = window->portRect;
- DrawPicture( pic, &r );
-
- EndUpdate( window );
- }
-
-
- /************************************* DoDialog */
-
- void DoDialog( void )
- {
- DialogPtr dialog;
- Boolean dialogDone = false;
- short itemHit, itemType;
- Handle itemHandle;
- Rect itemRect;
-
- short curRadioButton;
- PicHandle pic;
-
- dialog = GetNewDialog( kDialogResID, NULL, kMoveToFront );
-
- ShowWindow( dialog );
- SetPort( dialog );
-
- SetDialogDefaultItem( dialog, ok );
- SetDialogCancelItem( dialog, cancel );
-
- curRadioButton = gCurrentPICT - kBaseResID + kFirstRadio;
- GetDItem( dialog, curRadioButton, &itemType, &itemHandle, &itemRect );
- SetCtlValue( (ControlHandle)itemHandle, kOn );
-
- if ( gShowPreview )
- {
- GetDItem( dialog, iShowPreview, &itemType, &itemHandle, &itemRect );
- SetCtlValue( (ControlHandle)itemHandle, kOn );
- }
-
- DrawPreview( dialog, curRadioButton + kBaseResID - kFirstRadio );
-
- while ( ! dialogDone )
- {
- ModalDialog( NULL, &itemHit );
-
- switch( itemHit )
- {
- case ok:
- case cancel:
- dialogDone = true;
- break;
- case iShowPreview:
- GetDItem( dialog, iShowPreview, &itemType,
- &itemHandle, &itemRect );
- FlipControl( (ControlHandle)itemHandle );
-
- DrawPreview( dialog, curRadioButton + kBaseResID - kFirstRadio );
- break;
- case iAfghan:
- case iElephant:
- case iSquirrel:
- if ( curRadioButton != itemHit )
- {
- GetDItem( dialog, curRadioButton, &itemType,
- &itemHandle, &itemRect );
- FlipControl( (ControlHandle)itemHandle );
-
- GetDItem( dialog, itemHit, &itemType,
- &itemHandle, &itemRect );
- FlipControl( (ControlHandle)itemHandle );
-
- curRadioButton = itemHit;
-
- DrawPreview( dialog, curRadioButton + kBaseResID - kFirstRadio );
- }
- break;
- }
- }
-
- HideWindow( dialog );
-
- if ( itemHit == ok )
- {
- GetDItem( dialog, iShowPreview, &itemType,
- &itemHandle, &itemRect );
- if ( GetCtlValue( (ControlHandle)itemHandle ) == kOn )
- gShowPreview = true;
- else
- gShowPreview = false;
-
- if ( gCurrentPICT != curRadioButton +
- kBaseResID - kFirstRadio )
- {
- gCurrentPICT = curRadioButton +
- kBaseResID - kFirstRadio;
- SwitchPICT();
- }
- }
-
- DisposDialog( dialog );
- }
-
-
- /************************************* FlipControl */
-
- void FlipControl( ControlHandle control )
- {
- SetCtlValue( control, ! GetCtlValue( control ) );
- }
-
-
- /************************************* DrawPreview */
-
- void DrawPreview( DialogPtr dialog, short picID )
- {
- PicHandle pic;
- short itemHit, itemType;
- Handle itemHandle;
- Rect itemRect;
-
- GetDItem( dialog, iShowPreview, &itemType, &itemHandle, &itemRect );
- if ( GetCtlValue( (ControlHandle)itemHandle ) == kOff )
- {
- GetDItem( dialog, iUserItem, &itemType, &itemHandle, &itemRect );
- EraseRect( &itemRect );
- return;
- }
-
- pic = LoadPICT( picID );
-
- GetDItem( dialog, iUserItem, &itemType, &itemHandle, &itemRect );
- FrameRect( &itemRect );
-
- InsetRect( &itemRect, 1, 1 );
- DrawPicture( pic, &itemRect );
- }
-
-
- /************************************* SwitchPICT */
-
- void SwitchPICT( void )
- {
- WindowPtr window;
-
- window = FrontWindow();
- DisposeWindow( window );
-
- CreateWindow();
- }